from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.vo.employee_vo import EmployeeModel
from module_admin.entity.do.employee_do import EmployeeInfo
from sqlalchemy import bindparam, func, or_, select, update
from utils.page_util import PageUtil
from module_admin.entity.do.dept_do import SysDept


class EmployeeDao:
    """
    用户管理模块数据库操作层
    """

    @classmethod
    async def get_employee_info_for_edit_option(cls, db: AsyncSession, employee_info: EmployeeModel, data_scope_sql: str):
        """
        获取员工编辑对应的在用部门列表信息

        :param db: orm对象
        :param employee_info: 员工对象
        :param data_scope_sql: 数据权限对应的查询sql语句 SQLAlchemy 的异步查询语法
        查询所有未被删除的员工信息，并按 ID 排序返回模型对象列表。
        .scalars() 当你执行的是类似 select(Model) 的语句，返回的每一行都是一个完整的模型对象（如 EmployeeInfo 实例），而不是单独的字段值时，就需要使用 .scalars() 来提取这些对象。
        .all() 是用来 获取查询结果的所有行，并以列表的形式返回
        :return: 员工列表信息
        """
        employee_result = (
            (
        await db.execute(
            select(EmployeeInfo)
            .where(
                EmployeeInfo.del_flag == '0',
                EmployeeInfo.status == '0'  # 在职状态
            )
            .order_by(EmployeeInfo.id)
        )
            )
            .scalars()
            .all()
        )

        return employee_result

    @classmethod
    async def get_employee_list(cls, db: AsyncSession, page_object: EmployeeModel, data_scope_sql: str, is_page: bool = False):
        """
        根据查询参数获取部门列表信息

        :param db: orm对象
        :param page_object: 不分页查询参数对象
        :param data_scope_sql: 数据权限对应的查询sql语句
        :param is_page: 是否开启分页
        :return: 部门列表信息对象
        """
        query = (
            select(EmployeeInfo)
            .where(
                EmployeeInfo.del_flag == '0',
                EmployeeInfo.id == page_object.id if page_object.id is not None else True,
                EmployeeInfo.status == page_object.status if page_object.status else True,
                EmployeeInfo.name.like(f'%{page_object.name}%') if page_object.name else True,
                eval(data_scope_sql),
            )
            .order_by(EmployeeInfo.id)
            .distinct()
        )
        employee_list = await PageUtil.paginate(db, query, page_object.page_num, page_object.page_size, is_page)

        return employee_list
    
    @classmethod
    async def add_employee(cls, db: AsyncSession, employee: EmployeeModel):
        """
        新增员工信息
        :param db: orm对象
        :param employee: 员工信息对象
        :return: 新增员工的主键ID或完整对象
        """
        employee_dict = {
            'name': employee.name,
            'id_card': employee.id_card,
            'phone': employee.phone,
            'hire_date': employee.hire_date,
            'resignation_date': employee.resignation_date,
            'status': employee.status,
            'del_flag': employee.del_flag,
            'create_by': employee.create_by,
            'create_time': employee.create_time,
            'update_by': employee.update_by,
            'update_time': employee.update_time,
        }
        db_employee = EmployeeInfo(**employee_dict)
        db.add(db_employee)
        await db.flush()
        return db_employee
    
    # 方式二：
        # employee_info = EmployeeInfo(
        #     name=employee.name,
        #     id_card=employee.id_card,
        #     phone=employee.phone,
        #     hire_date=employee.hire_date,
        #     status=employee.status,
        #     del_flag='0',
        #     create_by=employee.create_by,
        #     create_time=employee.create_time,
        #     update_by=employee.update_by,
        #     update_time=employee.update_time
        # )
        # db.add(employee_info)
        # await db.commit()
        # await db.refresh(employee_info)
        # return employee_info